import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/internal/operators/map';
import { AnyJson } from './data-types/common.type';
import { ServicesModule } from './services.module';
declare var aesUtil: any;

@Injectable({
  providedIn: ServicesModule,
})
export class EducationService {
  constructor(private http: HttpClient) {}
  /**
   * @description: 登录红色教育
   */

  educationLogin(params: AnyJson): Observable<AnyJson> {
    const encryptKey = '1kQdPy54X23VjXT7f9nl6d4f2m8238ET';
    const aesRandomKey = aesUtil.getKey();
    const aesKey = aesUtil.encrypt(aesRandomKey, encryptKey);
    const data = aesUtil.encrypt(params, aesRandomKey);
    const body = new URLSearchParams();
    body.set('aesKey', aesKey);
    body.set('data', data);
    body.set('timestamp', new Date().getTime().toString());
    const headers = {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    };
    return this.http
      .post('https://contest.snzo.cn/user/api/ValidateCardByPass/', body, {
        headers,
      })
      .pipe(map((res) => res as AnyJson));
  }

  /**
   * @description: 获取验证码
   */
  checkEducationUser(body: AnyJson): Observable<any> {
    return this.http.post(
      'https://contest.snzo.cn/knowledge/red/api/getUserName',
      body
    );
  }
}
